package com.example.demo.contoller;

import javax.validation.Valid;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.TreeEntity;
import com.example.demo.enums.ApiErrorEnum;
import com.example.demo.exception.ApiException;
import com.example.demo.reqvo.PagehelperVo;
import com.example.demo.reqvo.ReqAddVo;
import com.example.demo.reqvo.ReqUpdateVo;
import com.example.demo.service.TreeService;
import com.example.demo.util.ResultEntity;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@RestController
@RequestMapping("/TreeController")
public class TreeController {
	@Autowired
	private TreeService treeService;

//	F5：跳入方法
//	F6：向下逐行调试
//	F7：跳出方法
//	F8：直接跳转到下一个断点，如果只有一个断点，则调到第二次执行该断点的地方 
	@ApiOperation(value = "添加")
	@PostMapping(value = "/add")
	@ApiResponses({ @ApiResponse(code = 200, message = "OK"), @ApiResponse(code = 400, message = "请求参数没填好"),
			@ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对") })
	public ResultEntity<Void> add( @RequestBody @Valid ReqAddVo req) {
		TreeEntity treeEntity = new TreeEntity();
		BeanUtils.copyProperties(req, treeEntity);
		treeService.add(treeEntity);
		return new ResultEntity<Void>();
	}

	@ApiOperation(value = "修改")
	@PostMapping(value = "/update")
	@ApiResponses(value = { @ApiResponse(code = 200, message = "OK") })
	public ResultEntity<Void> update(@RequestBody @Valid ReqUpdateVo reqUpdateVo) {
		TreeEntity treeEntity = new TreeEntity();
		BeanUtils.copyProperties(reqUpdateVo, treeEntity);
		treeService.update(treeEntity);
		return new ResultEntity<Void>();
	}

	@ApiOperation(value = "查询")
	@PostMapping(value = "/query")
	@ApiResponses(value = { @ApiResponse(code = 200, message = "OK") })
	public ResultEntity<TreeEntity> query(@RequestParam(value = "id", required = true) long id) {
		return new ResultEntity<TreeEntity>(treeService.query(id));
	}

	@ApiOperation(value = "删除")
	@PostMapping(value = "/delete")
	@ApiResponses(value = { @ApiResponse(code = 200, message = "OK") })
	public ResultEntity<Void> delete(@RequestParam(value = "id", required = true) long id) {
		treeService.delete(id);
		return new ResultEntity<Void>();
	}

	@ApiOperation(value = "分页查询")
	@PostMapping(value = "/pagehelperQuery")
	public ResultEntity<PageInfo<TreeEntity>> list(@ModelAttribute("UserSearchReq") PagehelperVo pagehelperVo) {
		return new ResultEntity<>(treeService.pagehelperQuery(pagehelperVo));
	}

}
